package com.kristin.thread.threadcreate;

import java.util.concurrent.*;

// 创建线程
public class ThreadCreate {

    public static class MyThread extends Thread {

        public void run() {
            System.out.println("继承Thread的方式创建线程");
        }

    }

    public static void main(String[] args) {
//
//        // 1. 继承Thread的方式来创建线程
//        Thread thread = new MyThread();
//        thread.start();
//
//        // 2. 实现runnable的方式来创建线程
//        new Thread(() -> {
//            System.out.println("runnable方式创建线程");
//        }).start();


        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0 ; i < 20 ; i ++) {
                    System.out.println("执行任务：" + i);
                    if(Thread.currentThread().isInterrupted()) {
                        System.out.println("检测到中断，任务停止执行");
                        break;
                    }
                }
            }
        });
        t.start();
        t.interrupt();
        System.out.println(t.isInterrupted());
        System.out.println(t.isAlive());

        Thread.currentThread().interrupt();
        while(true) {
            System.out.println("主线程执行");
            if(Thread.currentThread().isInterrupted()) {
                System.out.println("检测到主线程中断，停止主线程执行");
                break;
            }
        }

    }

}
